//
//  UserRequest.m
//  coffee
//
//  Created by student4 on 2020/11/17.
//  Copyright © 2020 MOSAD. All rights reserved.
//

#include "UserRequest.h"
#import "AFNetworking.h"

@interface UserReq()
@property (nonatomic,assign)NSString *host;
@property (nonatomic,retain)AFHTTPSessionManager *manager;
@end

UserReq *default_user_request;

@implementation UserReq
- (void)initReq:(RequestConfig*)config{
    _host = [config host];
    _manager = [config manager];
}
+ (id)defaultUserRequest{
    if (default_user_request == nil) {
        default_user_request = [[UserReq alloc]init];
        [default_user_request initReq:[RequestConfig defaultConfig]];
    }
    return default_user_request;
}
-(void)login:(LoginReq *)loginReq success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSDictionary *req = @{@"name":loginReq.name,@"password":loginReq.password};
    NSString *url =[NSString stringWithFormat:@"%@/user/login/pass",_host];
    _manager.responseSerializer = [AFJSONResponseSerializer serializer];
    _manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [_manager POST:url parameters:req headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
- (void)regist:(RegistReq *)registReq success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSDictionary *req = @{@"email":registReq.email,@"name":registReq.name,@"password":registReq.password};
    NSString *url = [NSString stringWithFormat:@"%@/user/register",_host];
    _manager.responseSerializer = [AFJSONResponseSerializer serializer];
    _manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [_manager POST:url parameters:req headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)getInfo:(NSString *)userID success:(void (^)(UserInfo *))success failure:(void (^)(NSString *))failure{
    NSString *url = [NSString stringWithFormat:@"%@/user/info/%@",_host,userID];
    [_manager GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            UserInfo *info = [[UserInfo alloc]initWith:responseObject];
            success(info);
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
-(void)rename:(NSString *)newName success:(void (^)(void))success failure:(void (^)(NSString *))failure{
    NSDictionary *req = @{@"name":newName};
    NSString *url = [NSString stringWithFormat:@"%@/user/name",_host];
    _manager.responseSerializer = [AFJSONResponseSerializer serializer];
    _manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [_manager POST:url parameters:req headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if([responseObject[@"State"] isEqualToString:@"success"]){
            success();
        }else{
            failure(responseObject[@"State"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure([NSString stringWithFormat:@"%@",error]);
    }];
}
@end
